package com.mxf.code.config;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 多数据源
 *
 * @author mxf
 * @since 1.0.0
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicContextHolder.peek();
    }

    public void addDataSources(Map<Object, Object> dataSources) {
        Map<Object, DataSource> resolvedDataSources = super.getResolvedDataSources();

        Map<Object, Object> targetDataSources = new HashMap<>(resolvedDataSources);
        targetDataSources.putAll(dataSources);

        super.setTargetDataSources(targetDataSources);

        super.afterPropertiesSet();
    }

}
